﻿
@{
    ViewBag.Title = "CivilPriceView";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
}
@section head{
    <script src="~/Scripts/exportJs.js"></script>
}
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>土建核价单列表</legend>
</fieldset>

<form class="layui-form" enctype="multipart/form-data">
    <div class="layui-form-item">
        <label class="layui-form-label">搜索</label>
        <div class="layui-input-block">
            <input type="text" id="SearchText" name="Title1" required lay-verify="required" onkeypress="getKey()" placeholder="输入土建核价单名称" class="layui-input" style="width:300px;float:left;">
            <input type="text" value="" style="display:none" />
            <button type="button" class="layui-btn layui-btn-normal" style="float:left;" onclick="SearchFunc()"><i class="layui-icon">&#xe615;</i></button>
            <div class="layui-form-mid layui-word-aux">高级搜索</div>
            <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="BtnAdd"><i class="layui-icon">&#xe61f;</i>新增</button>
            <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="BtnCopy"><i class="layui-icon">&#xe61f;</i>复制选中行</button>
            <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="BtnExport">导出选中行</button>
            @*<button type="button" class="layui-btn layui-btn-normal" onclick="apply()"><i class="layui-icon">&#xe63b;</i></button>*@
        </div>
    </div>
</form>
<table id="demo" lay-filter="CivilTableList"></table>
<div id="DivCopy" style="display:none;margin-top:5px;">
    <form class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">项目名称</label>
            <div class="layui-input-block">
                <input type="text" id="InputCopyName" class="layui-input" style="width:300px;float:left;">
                <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="BtnCopyAdd">确认复制</button>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    var table;
    layui.use(['form', 'table', 'upload', 'layer'], function () {
        var upload = layui.upload;
        table = layui.table;
        var tab = parent.tab;
        var layer = layui.layer;
        var index;
        var layerCopyIndex;
        var CivilPriceId;

        $("#BtnAdd").on("click", function () {
            //$("#addCardFrom").css("display", "block");
            //index = layer.open({
            //    type: 2,
            //    area: ['700px', '200px'],
            //    content: '/Project/InfoAdd' //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            //});
            parent.layui.index.openTabsPage('/PreEstimate/CivilPriceSingle', '新增土建核价单');
        });

        $("#BtnSave").on("click", function () {
            layer.close(index);
        });

        $("#BtnCopy").on("click", function () {


            var checkStatus = table.checkStatus('demo');
            var data = checkStatus.data;
            if (data.length != 1) {
                layer.msg('只需要选择1行数据', { icon: 2, time: 1500 });
                return;
            }
            $("#InputCopyName").val(data[0].Name);
            CivilPriceId = data[0].CivilPriceId;
            layerCopyIndex = layer.open({
                type: 1,
                area: ['550px', '110px'],
                content: $("#DivCopy"), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            });

            //layer.alert(JSON.stringify(checkStatus.data));

        });

        $("#BtnExport").on("click", function () {
            var checkStatus = table.checkStatus('demo');
            var data = checkStatus.data;
            if (data.length < 1) {
                layer.msg('至少选择一行数据', { icon: 3, time: 1500 });
                return;
            }
            var arr = new Array();
            
            for (var i = 0; i < data.length; i++) {
                arr[i] = data[i].CivilPriceId;
            }
            var formstr = "";
            for (var i = 0; i < data.length; i++) {
                formstr += "CivilPriceId::" + data[i].CivilPriceId + "&";
            }
            formstr = formstr.substr(0, formstr.length - 1);

            $.down('/PreEstimate/CivilExport', formstr, 'post');
            //$.ajax({
            //    type: "POST",
            //    url: "/PreEstimate/CivilExport",
            //    //contentType: "application/json",
            //    //dataType: "json",
            //    async: false,
            //    data: { CivilPriceIds: arr },
            //    success: function (msg) {
            //        if (msg.result == "OK") {
            //            layer.msg('复制成功"', { icon: 6, time: 1000 });
            //            table.reload("demo", {
            //                //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
            //            });
            //            layer.close(layerCopyIndex);
            //        } else {
            //            alert(msg.result);
            //        }
            //    }
            //});


        });

        $("#BtnCopyAdd").on('click', function () {
            var tempname = $("#InputCopyName").val();
            tempname = tempname.replace(/\s*/g, "");
            if (tempname == "") {
                return;
            }
            $.ajax({
                type: "POST",
                url: "/PreEstimate/CivilCopy",
                async: false,
                data: { "CivilPriceId": CivilPriceId, "Name": tempname },
                success: function (msg) {
                    if (msg.result == "OK") {
                        layer.msg('复制成功"', { icon: 6, time: 1000 });
                        table.reload("demo", {
                            //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                        });
                        layer.close(layerCopyIndex);
                    } else {
                        alert(msg.result);
                    }
                }
            });
        });

        //执行实例
        var uploadInst = upload.render({
            elem: '#UpLoadExcel' //绑定元素
            , url: '/GoodsInfo/Upload/' //上传接口
            , accept: 'file'
            , size: 10240
            , exts: "xls|xlsx"
            , done: function (res) {
                //上传完毕回调
                alert(res.error);
                table.reload("demo", {
                    //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                });
            }
            , error: function () {
                //请求异常回调
                alert("请求异常");
            }
        });

        ////监听单元格编辑
        //table.on('edit(UserTableList)', function (obj) {
        //    var value = obj.value //得到修改后的值
        //        , data = obj.data //得到所在行所有键值
        //        , field = obj.field; //得到字段
        //    layer.msg('[ID: ' + data.id + '] ' + field + ' 字段更改为：' + value);
        //});

        //第一个实例
        table.render({
            elem: '#demo'
            , method: 'post'
            //, height: 315
            , url: '/PreEstimate/CivilTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "CivilPriceId", Order: "Desc", Condition: " and IsDelete<>1" }
            , limits: [10, 50, 100]
            , cols: [[ //表头
                { type: 'checkbox' }
                , { field: 'Name', title: '项目名称', width: 200 }
                , { field: 'TaxRate', title: '税率', width: 100 }
                , { field: 'Unit', title: '单位', width: 100 }
                , { field: 'UnitPrice', title: '单价', width: 150 }
                , { field: 'Memo', title: '备注', width: 150 }
                , {
                    field: "operation", title: "操作", width: 180,
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="detail">编辑</a><a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }
            ]]
        });


        //鼠标双击事件回调
        table.on('rowDouble(CivilTableList)', function (obj) {
            var data = obj.data;
            index = layer.open({
                type: 2,
                area: ['1200px', '500px'],
                content: '/PreEstimate/CivilBriefView?CivilPriceId=' + data.CivilPriceId, //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            });
            //alert(data);
        });

        table.on("tool(CivilTableList)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'detail') {
                parent.layui.index.openTabsPage('/PreEstimate/CivilPriceSingleEdit?CivilPriceId=' + data.CivilPriceId, '项目信息编辑');

            }


            if (layEvent == 'TableDel') {
                layer.confirm('<span class="layui-icon"></span>' + '是否真的要删除“' + data.Name + '”？', {
                    btn: ['确定删除', '取消'] //可以无限个按钮
                    , btn1: function (index) {
                        //按钮【删除】的回调
                        //SSEditDelete
                        $.ajax({
                            type: "POST",
                            url: "/PreEstimate/CivilPriceDel",
                            async: false,
                            data: { "CivilPriceId": data.CivilPriceId },
                            success: function (msg) {
                                if (msg.result == "OK") {
                                    layer.msg('删除成功"', { icon: 6, time: 1000 });
                                    table.reload("demo", {
                                        //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                                    });
                                    //tab.close(tab.getId());
                                } else {
                                    alert("重置失败");
                                }
                            }
                        });
                        layer.close(index);
                    }
                    , btn2: function (index) {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }

        });

    });

    //回车键
    function getKey() {
        if (event.keyCode == 13) {
            SearchFunc();
        }
    }

    function SearchFunc() {
        var text = $("#SearchText").val();
        if (text == "") {
            table.reload("demo", {
                where: {
                    SortName: "CivilPriceId", Order: "Desc", Condition: " and IsDelete<>1"
                },
                page: {
                    curr: 1
                }
            });
        }
        else {
            table.reload("demo", {
                where: {
                    SortName: "CivilPriceId", Order: "Desc", Condition: " and IsDelete<>1 and Name like '%" + text + "%'"
                },
                page: {
                    curr: 1
                }
            });
        }
    }



    function dealNumber(money) {
        if (money && money != null) {
            money = String(money);
            var left = money.split('.')[0], right = money.split('.')[1];
            right = right ? (right.length >= 2 ? '.' + right.substr(0, 2) : '.' + right + '0') : '.00';
            var temp = left.split('').reverse().join('').match(/(\d{1,3})/g);
            return (Number(money) < 0 ? "-" : "") + temp.join(',').split('').reverse().join('') + right;
        } else if (money === 0) {   //注意===在这里的使用，如果传入的money为0,if中会将其判定为boolean类型，故而要另外做===判断
            return '0.00';
        } else {
            return "";
        }
    };




</script>
